package com.pri.chat.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.netease.nim.uikit.common.util.string.StringUtil;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.model.CodeModel;
import com.pri.chat.utils.Des3Util;
import com.vondear.rxtool.view.RxToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 绑定手机号
 */
public class BindMobieActivity extends BaseActivity {

    @BindView(R.id.mobileEdit)
    EditText mobileEdit;
    @BindView(R.id.codeEdit)
    EditText codeEdit;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.submit)
    View submit;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_mobile;
    }

    @Override
    protected void initView() {
        setTitle("绑定手机号");
    }

    @Override
    protected void initData() {
        time = new TimeCount(60000, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isFinish = true;
        time.cancel();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String mobile = SharedHelper.readMobile(this);
        if(!StringUtil.isEmpty(mobile)){
            mobileEdit.setText(mobile);
        }
    }

    /**
     * 绑定手机号
     */
    private void bindMobile() {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            SharedHelper.saveMobile(this,mobileEdit.getText().toString());
            RxToast.normal("绑定成功！");
            finish();
        };
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",SharedHelper.readId(this));
        params.put("sendId",mCodeId);
        params.put("code",codeEdit.getText().toString());
        params.put("mobile",mobileEdit.getText().toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().bindMobile(new ProgressSubscriber<>(onNextListener, this, false), requestBody);
    }

    @OnClick({R.id.tv_code,R.id.submit})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.tv_code:
                if (mobileEdit.getText().toString().isEmpty()) {
                    RxToast.normal("请输入手机号");
                    return;
                }
                sendCode(mobileEdit.getText().toString());
                break;
            case R.id.submit:
                if(mobileEdit.getText().toString().isEmpty()) {
                    RxToast.normal("请输入手机号");
                    return;
                }
                if(codeEdit.getText().toString().isEmpty()) {
                    RxToast.normal("请输入验证码");
                    return;
                }
                bindMobile();
                break;
        }
    }
    private TimeCount time;
    private String mCodeId;
    private boolean isFinish = false;
    /**
     * 发送验证码
     * @param mobile 手机号
     */
    private void sendCode(String mobile) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            mCodeId = (String) baseBean.getData();
            time.start();
            RxToast.normal("验证码发送成功");
        };
        CodeModel codeModel = new CodeModel(mobile, 2);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(codeModel));
        HttpMethods.getInstance().sendCode(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }

    class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

            tv_code.setClickable(false);
            tv_code.setText(millisUntilFinished / 1000 + "秒后可发送");
        }

        @Override
        public void onFinish() {
            if (!isFinish) {
                tv_code.setText("重新获取");
                tv_code.setClickable(true);
            }

        }
    }
}
